package com.ChenYaRou.www.dao.impl;

import com.ChenYaRou.www.dao.MessageDao;
import com.ChenYaRou.www.po.Message;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JdbcUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName MessageDaoImpl
 * @Description TODO
 * @Author 86183
 * @Date 2022/4/13 19:25
 * @Version 1.0
 **/
public class MessageDaoImpl implements MessageDao {

    /**
     * 新建留言
     * @param message 新留言
     * @return 更新条数
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @Override
    public int insert(Message message) throws IllegalAccessException, SQLException {
        return JdbcUtil.getInstance().insertOrUpdate(GenerateSql.getInstance().insert(message));
    }

    /**
     * update message state
     * @param newMessage message更新的值
     * @param  oldMessage old message
     * @return 成功与否
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @Override
    public int update(Message newMessage, Message oldMessage) throws IllegalAccessException, SQLException {
        return JdbcUtil.getInstance().insertOrUpdate(GenerateSql.getInstance().update(newMessage,oldMessage));
    }

    /**
     * 删除留言
     * @param messageId 留言id
     * @return 成功与否
     * @throws SQLException
     */
    @Override
    public int delete(int messageId) throws SQLException {
        String sql="delete from message where id="+messageId;
        return JdbcUtil.getInstance().insertOrUpdate(sql);
    }

    /**
     * 查找message集合
     * @param message 条件message
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws NoSuchFieldException
     * @throws InstantiationException
     */
    @Override
    public List<Message> selectMore(Message message) throws IllegalAccessException, SQLException, NoSuchFieldException, InstantiationException {
        return JdbcUtil.getInstance().selectMore(GenerateSql.getInstance().select(message), Message.class);
    }
}
